//
//  DetialNewsViewController.m
//  YiLiao
//
//  Created by Infinite on 16/7/2.
//  Copyright © 2016年 Infinite. All rights reserved.
//

#import "DetialNewsViewController.h"
#import "DetailTableViewCell.h"
#import "Detial2TableViewCell.h"
#import "HeadTableViewCell.h"
#import "FootTableViewCell.h"

#import <AFNetworking.h>

@interface DetialNewsViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSDictionary * _allNewsDic;
}

@property (nonatomic,strong) UITableView * tabelView;

@end

@implementation DetialNewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self AFNet];
    
    self.tabelView =[[UITableView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight) style:(UITableViewStylePlain)];
    _tabelView.dataSource=self;
    _tabelView.delegate=self;
    [_tabelView registerNib:[UINib nibWithNibName:@"HeadTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"headCell"];
    [_tabelView registerNib:[UINib nibWithNibName:@"DetailTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"detialCell"];
    [_tabelView registerNib:[UINib nibWithNibName:@"Detial2TableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"detialCell2"];
    [_tabelView registerNib:[UINib nibWithNibName:@"FootTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"footCell"];
    
    [self.view addSubview:self.tabelView];
    
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray * array = [_allNewsDic objectForKey:@"data"];
    NSArray * array1 = [_allNewsDic objectForKey:@"comments"];
    if (section==0) {
        return 1;
    }else if (section==1) {
        return array.count;
    }else{
        return array1.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    if (indexPath.section==0) {
        HeadTableViewCell * headCell = [tableView dequeueReusableCellWithIdentifier:@"headCell" forIndexPath:indexPath];
        NSDictionary * infoDic = [_allNewsDic objectForKey:@"info"];
        
        headCell.titleTextLable.text=[infoDic objectForKey:@"news_title"];
        headCell.nameTextLabel.text=[NSString stringWithFormat:@"%@  %@",[infoDic objectForKey:@"source"],[infoDic objectForKey:@"auther"]];
        headCell.timeTextLabel.text=[infoDic objectForKey:@"time"];
        
        return headCell;
    }else if (indexPath.section==1){
        NSArray * dataArray = [_allNewsDic objectForKey:@"data"];
        NSDictionary * dataDic = [dataArray objectAtIndex:indexPath.row];
        if ([[dataDic objectForKey:@"data_type"] isEqualToString:@"1"]) {
            DetailTableViewCell * detialCell = [tableView dequeueReusableCellWithIdentifier:@"detialCell" forIndexPath:indexPath];
            detialCell.contentTextLabel.text=[dataDic objectForKey:@"content"];
            return detialCell;
        }else{
            Detial2TableViewCell * detialCell2 = [tableView dequeueReusableCellWithIdentifier:@"detialCell2" forIndexPath:indexPath];
            NSDictionary * iamgeDic = [dataDic objectForKey:@"image"];
            NSString * heightStr =[iamgeDic objectForKey:@"height"];
            CGFloat height= [heightStr floatValue];
            detialCell2.DEImageView.frame=CGRectMake(0, 0, detialCell2.frame.size.width,height);
            detialCell2.DEImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080%@",[iamgeDic objectForKey:@"source"]]]]];
            
            return detialCell2;
        }
        
        
    }
    NSArray * commentsArray = [_allNewsDic objectForKey:@"comments"];
    NSDictionary * indxeDic = [commentsArray objectAtIndex:indexPath.row];
    FootTableViewCell * footCell = [tableView dequeueReusableCellWithIdentifier:@"footCell" forIndexPath:indexPath];
    
    footCell.nameTextLabel.text=[indxeDic objectForKey:@"name"];
    footCell.chatTextLabel.text=[indxeDic objectForKey:@"info"];
    
    
    return footCell;
    
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 2) {
        return @"热点评论";
    }
    return nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)AFNet{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes=[manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"text/plain",nil]];
    NSString * string = [NSString stringWithFormat:@"http://localhost:8080%@",self.Sourceurl];
    
    [manager GET:string parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        _allNewsDic=responseObject;
        [self.tabelView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray * dataArray = [_allNewsDic objectForKey:@"data"];
    NSDictionary * dataDic = [dataArray objectAtIndex:indexPath.row];
    if (indexPath.section==1) {
        if ([[dataDic objectForKey:@"data_type"]isEqualToString:@"1"]) {
            NSString * dataStr = [dataDic objectForKey:@"content"];
            CGRect rect =[dataStr boundingRectWithSize:CGSizeMake(ScreenWidth, 1000) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15] } context:nil];
            return rect.size.height;
        }else{
            NSDictionary * imageDic = [dataDic objectForKey:@"image"];
            NSString *str =[imageDic objectForKey:@"height"];
            CGFloat height = [str floatValue];
            
            return height;
        }

    }else if (indexPath.section == 0){
       return 100;
    }else{
        return 60;
    }
    
    
   
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
